package io.renren.modules.shop.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.modules.shop.entity.AddAddrEntity;
import io.renren.modules.shop.entity.CustomerEntity;
import io.renren.modules.shop.service.CustomerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.shop.entity.AddrEntity;
import io.renren.modules.shop.service.AddrService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 *
 *
 * @author Yangqianyong
 * @email sunlightcs@gmail.com
 * @date 2021-11-17 15:35:51
 */
@RestController
@RequestMapping("shop/addr")
public class AddrController {
    @Autowired
    private AddrService addrService;
    @Autowired
    private CustomerService customerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("shop:addr:list")
    public R list(@RequestParam Map<String, Object> params, @RequestParam Long id){
        PageUtils page = addrService.queryPage(params,id);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{addrId}")
//    @RequiresPermissions("shop:addr:info")
    public R info(@PathVariable("addrId") Long addrId){
		AddrEntity addr = addrService.getById(addrId);

        return R.ok().put("addr", addr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("shop:addr:save")
    public R save(@RequestBody AddrEntity addr){
		addrService.save(addr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("shop:addr:update")
    public R update(@RequestBody AddrEntity addr){
		addrService.updateById(addr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("shop:addr:delete")
    public R delete(@RequestBody Long[] addrIds){
		addrService.removeByIds(Arrays.asList(addrIds));
        return R.ok();
    }

    @RequestMapping("/addAddr")
    public R addAddr(@RequestBody AddAddrEntity addr){
        AddrEntity addrEntity = new AddrEntity();
        addrEntity.setCustomerId(addr.getCustomerId());
        addrEntity.setAddress(addr.getAddress());
        addrEntity.setAddrDefault(0);
        addrEntity.setUserName(addr.getUserName());
        addrEntity.setMobilePhone(addr.getMobilePhone());
        addrService.save(addrEntity);
        return R.ok();
    }

    @RequestMapping("/deleteAddr")
    public R deleteAddr(@RequestParam Long id){
        addrService.removeById(id);
        return R.ok();
    }
}
